﻿using System;
using System.Threading;
using System.Windows.Forms;
using MS.Internal.Mita.Foundation;

namespace UIAStudio
{
    public class UIATreeView : TreeView
    {
        ContextMenu _contextMenu;
        MenuItem refreshItem;

        WorkItem _endUpdateWorkItem;

        public UIATreeView()
        {
            refreshItem = new MenuItem(StringResource.Get("UIATreeView_ContextMenu_Refresh"), new EventHandler(RefreshChildNodes));
            _contextMenu = new ContextMenu(new MenuItem[]{refreshItem});

            this.ContextMenu = _contextMenu;

            _endUpdateWorkItem = new CallbackWorkItem(WorkQueue, new ThreadStart(AsyncEndUpdate));
            //base.BeforeSelect += new TreeViewCancelEventHandler(PopulateNode);
            //base.BeforeSelect += new TreeViewCancelEventHandler(UIATreeView_PopulateTree);
            base.BeforeExpand += new TreeViewCancelEventHandler(UIATreeView_PopulateTree);
            
            //base.AfterSelect += new TreeViewEventHandler(UIATreeView_AfterSelect);
        }

        void UIATreeView_PopulateTree(object sender, TreeViewCancelEventArgs e)
        {
            UIATreeNode node = e.Node as UIATreeNode;
            if (node != null && MS.Internal.Mita.Foundation.Keyboard.Instance.GetKeyState(MS.Internal.Mita.Foundation.Keyboard.LockKey.ScrollLock) != MS.Internal.Mita.Foundation.Keyboard.KeyState.Toggled)
            {
                PopulationWorkItem populationWorkItem = new PopulationWorkItem(WorkQueue, node);
                WorkQueue.Enqueue(populationWorkItem);
            }
        }

        void AsyncEndUpdate()
        {
            ThreadStart ts = delegate
            {
                base.EndUpdate();
            };
            this.Invoke(ts);
        }

        public void SearchTree(UIObject item)
        {
            UIATreeNode root = base.Nodes[0] as UIATreeNode;
            base.BeginUpdate();
            SearchWorkItem searchWorkItem = new SearchWorkItem(WorkQueue, root, item);
            WorkQueue.Enqueue(searchWorkItem);
            WorkQueue.Enqueue(_endUpdateWorkItem);            
        }

        //private void UIATreeView_AfterSelect(object sender, TreeViewEventArgs e)
        //{
        //    UIATreeNode node = e.Node as UIATreeNode;
        //    HighlightWorkItem lighterWorkItem = new HighlightWorkItem(WorkQueue, node.UIObject);
        //    WorkQueue.Enqueue(lighterWorkItem);


        //    node.TreeView.SelectedNode.BackColor = System.Drawing.Color.Blue;
        //}

        private void RefreshChildNodes(object target, EventArgs args)
        {
            UIATreeNode node = target as UIATreeNode;
            PopulationWorkItem populationWorkItem = new PopulationWorkItem(WorkQueue, node);
            WorkQueue.Enqueue(populationWorkItem);
        }

        public void Initialize()
        {
            InitUIATreeViewWorkItem initWorkItem = new InitUIATreeViewWorkItem(WorkQueue, this);
            WorkQueue.Enqueue(initWorkItem);
        }

        //public void PopulateNode(object sender, TreeViewCancelEventArgs args)
        //{
        //    UIATreeNode node = args.Node as UIATreeNode;
        //    UIATreeNode firstChild = node.Nodes[0] as UIATreeNode;
        //    if (firstChild.UIObject == null)
        //    {   
        //        PopulationWorkItem populationWorkItem = new PopulationWorkItem(WorkQueue, node);
        //        WorkQueue.Enqueue(populationWorkItem);
        //    }
        //}

        public WorkQueue WorkQueue
        {
            get
            {
                return Helper.GetWorkQueue();
            }
        }
        
    }
}
